package com.haidechizi.fastadmin.modules.sys.controller;

import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.modules.sys.entity.SysDict;
import com.haidechizi.fastadmin.modules.sys.service.SysDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 数据字典
 */
@RestController
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public Response list(PageDto pageDto, SysDict sysDict) {
        PageInfo page = sysDictService.queryPage(pageDto, sysDict);

        return Response.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public Response info(@PathVariable("id") Long id) {
        SysDict dict = sysDictService.selectById(id);

        return Response.ok().put("dict", dict);
    }

    /**
     * 信息
     */
    @RequestMapping("/typeInfo/{id}")
    @RequiresPermissions("sys:dict:info")
    public Response typeInfo(@PathVariable("id") Long id) {
        SysDict dict = sysDictService.selectById(id);
        if(dict != null) {
            dict.setId(null);
            dict.setValue("");
            dict.setLabel("");
            dict.setOrderNum(dict.getOrderNum() + 10);
        }
        return Response.ok().put("dict", dict);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public Response save(@RequestBody SysDict dict) {
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.insert(dict);

        return Response.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public Response update(@RequestBody SysDict dict) {
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.updateById(dict);

        return Response.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public Response delete(@RequestBody Long[] ids) {
        sysDictService.deleteBatchIds(Arrays.asList(ids));

        return Response.ok();
    }

}
